package com.example.androidmainshixun.view;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.androidmainshixun.R;
import com.example.androidmainshixun.adapter.Adapter1;
import com.example.androidmainshixun.entity.Goods1;

import java.util.ArrayList;
import java.util.List;

public class Title1Fragment extends FirstFragment {

    private RecyclerView recyclerView;
    private Adapter1 adapter1; // 假设这是您的自定义适配器
    private List<Goods1> goodsList = new ArrayList<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 初始化商品数据
        initializeGoodsList();
    }

    private void initializeGoodsList() {
        goodsList.add(new Goods1("橙子", 10.5, R.drawable.f1));
        goodsList.add(new Goods1("白菜", 0.3, R.drawable.image2));
        goodsList.add(new Goods1("荔枝", 13.5, R.drawable.image));
        goodsList.add(new Goods1("苹果", 10.3, R.drawable.f4));
        goodsList.add(new Goods1("蓝莓", 15.80, R.drawable.f2));
        goodsList.add(new Goods1("香蕉", 8.3, R.drawable.f3));
        // 添加更多商品...
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.title1_fragment, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView = view.findViewById(R.id.rv_goods);
        if (recyclerView == null) {
            // 处理错误，RecyclerView未找到
            return;
        }
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2)); // 设置为两列

        // 初始化适配器并设置给RecyclerView
        adapter1 = new Adapter1(goodsList);
        recyclerView.setAdapter(adapter1); // 只调用一次setAdapter
    }
}